//
//  UITextView+Placeholder.m
//  etionUI
//
//  Created by wjxfhxy on 14-9-15.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "XUITextView.h"

@interface XUITextView ()
{
    UILabel *_placeHoldLabel;
    
    UIToolbar *_toolBar;
}

@end

@implementation XUITextView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.font = [UIFont systemFontOfSize:16];
    self.contentInset = UIEdgeInsetsMake(-5, 0, 5, 0);

    return self;
}

-(void)dealloc
{
    [NSNotificationCenter removeObserver:self];
    [_toolBar release_mm];
    ReleaseObj(_placeHoldLabel);
    superdealloc_mm;
}

-(void)setText:(NSString *)text
{
    [super setText:text];

    [self textDidChanged];
}

-(void)setPlaceHolder:(NSString *)placeHolder
{
    if(_placeHoldLabel == nil)
    {
        _placeHoldLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 8, 200, 20)];
        _placeHoldLabel.backgroundColor = [UIColor clearColor];
        _placeHoldLabel.font = [UIFont systemFontOfSize:16];
        _placeHoldLabel.textColor = [UIColor colorWithR:190 G:190 B:190 A:1];
        [self addSubview:_placeHoldLabel];
        
        [NSNotificationCenter addObserver:self dictionarySelector:@{
                        UITextViewTextDidBeginEditingNotification:@"observerUITextViewTextDidBeginEditingNotification:",
                        UITextViewTextDidChangeNotification:@"observerUITextViewTextDidChangeNotification:",
                        UITextViewTextDidEndEditingNotification:@"observerUITextViewTextDidEndEditingNotification:"} object:self];
    }
    
    _placeHoldLabel.text = placeHolder;
    
    if(placeHolder.length <= 0)
    {
        [_placeHoldLabel removeFromSuperview];
        ReleaseObj(_placeHoldLabel);
        [NSNotificationCenter removeObserver:self arrayName:@[UITextViewTextDidBeginEditingNotification,UITextViewTextDidChangeNotification,UITextViewTextDidEndEditingNotification] object:nil];
    }
}

-(void)setPlaceHolderFont:(UIFont *)placeHolderFont
{
    if(_placeHoldLabel == nil)
        [self setPlaceHolder:@""];
    _placeHoldLabel.font = placeHolderFont;
}

-(void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    if(_placeHoldLabel == nil)
        [self setPlaceHolder:@""];
    _placeHoldLabel.textColor = placeHolderColor;
}

-(NSString*)placeHolder
{
    return _placeHoldLabel.text;
}

-(void)setHiddenDoneButton:(BOOL)hiddenDoneButton
{
    if(hiddenDoneButton == NO)
    {
        _toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 44)];
        UIBarButtonItem *item1 = [[[UIBarButtonItem alloc ]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease_mm];
        UIBarButtonItem *item2 = [[[UIBarButtonItem alloc ]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClick:)] autorelease_mm];
        _toolBar.items = @[item1,item2];
        self.inputAccessoryView = _toolBar;
    }
    else
    {
        ReleaseObj(_toolBar);
        self.inputAccessoryView = nil;
    }
}

-(void)observerUITextViewTextDidBeginEditingNotification:(NSNotification*)noti
{
    if(noti.object != self)
        return;
    
//    _placeHoldLabel.hidden = YES;
}

-(void)observerUITextViewTextDidChangeNotification:(NSNotification*)noti
{
    if(noti.object != self)
        return;
    
    [self textDidChanged];
}

-(void)textDidChanged
{
    if(self.text.length > 0)
        _placeHoldLabel.hidden = YES;
    else
        _placeHoldLabel.hidden = NO;
    
    if([UIDevice isIOS7Serial] == YES)
    {
        CGRect line = [self caretRectForPosition:self.selectedTextRange.start];
        CGFloat overflow = line.origin.y + line.size.height - ( self.contentOffset.y + self.bounds.size.height - self.contentInset.bottom - self.contentInset.top );
        if ( overflow > 0 )
        {
            CGPoint offset = self.contentOffset;
            offset.y += overflow;
            [self setContentOffset:offset];
        }
    }
}

-(void)observerUITextViewTextDidEndEditingNotification:(NSNotification*)noti
{
    if(noti.object != self)
        return;
    
    if(self.text.length <= 0)
        _placeHoldLabel.hidden = NO;
}

-(void)doneButtonClick:(id)sender
{
    [self resignFirstResponder];
}

@end
